//MINI UI解析
mini.parse();

/**
 * 订单信息查询处理器类
 */
function OrderQueryHandler() {

}

OrderQueryHandler.instance = new OrderQueryHandler();
OrderQueryHandler.mgrData = new OrderMgrData();
OrderQueryHandler.hiddenForm = new mini.Form("hiddenForm");
OrderQueryHandler.hiddenForm.loadingMsg = OrderQueryHandler.mgrData.loadingMsg;
OrderQueryHandler.orderQueryForm = new mini.Form("orderQueryForm");
OrderQueryHandler.boiOrder = mini.get("boiOrder");
OrderQueryHandler.boiPay = mini.get("boiPay");
OrderQueryHandler.boiShip = mini.get("boiShip");
OrderQueryHandler.boiCountry = mini.get("boiCountry");
OrderQueryHandler.boiProvince = mini.get("boiProvince");
OrderQueryHandler.boiCity = mini.get("boiCity");
OrderQueryHandler.boiDistrict = mini.get("boiDistrict");

/**
 * 加载订单状态
 * @param {} data
 */
OrderQueryHandler.loadBoiOrderData = function(data){
	OrderQueryHandler.boiOrder.setData(null);
	OrderQueryHandler.boiOrder.setData(data);
	OrderQueryHandler.boiOrder.setValue(OrderQueryHandler.mgrData.boiOrderData[0].id);
}

/**
 * 加载付款状态
 * @param {} data
 */
OrderQueryHandler.loadBoiPayData = function(data){
	OrderQueryHandler.boiPay.setData(null);
	OrderQueryHandler.boiPay.setData(data);
	OrderQueryHandler.boiPay.setValue(OrderQueryHandler.mgrData.boiPayData[0].id);
}

/**
 * 加载配送状态
 * @param {} data
 */
OrderQueryHandler.loadBoiShipData = function(data){
	OrderQueryHandler.boiShip.setData(null);
	OrderQueryHandler.boiShip.setData(data);
	OrderQueryHandler.boiShip.setValue(OrderQueryHandler.mgrData.boiShipData[0].id);
}

/**
 * 加载国家名称
 * @param {} data
 */
OrderQueryHandler.loadCountryData = function(data){
	OrderQueryHandler.boiCountry.setData(null);
	OrderQueryHandler.boiCountry.setData(data);
	OrderQueryHandler.boiCountry.setValue(OrderQueryHandler.mgrData.selectionData[0].id);
}

/**
 * 加载省份名称
 * @param {} data
 */
OrderQueryHandler.loadProvinceData = function(data){
	OrderQueryHandler.boiProvince.setData(null);
	OrderQueryHandler.boiProvince.setData(data);
	OrderQueryHandler.boiProvince.setValue(OrderQueryHandler.mgrData.selectionData[0].id);
}

/**
 * 加载城市名称
 * @param {} data
 */
OrderQueryHandler.loadCityData = function(data){
	OrderQueryHandler.boiCity.setData(null);
	OrderQueryHandler.boiCity.setData(data);
	OrderQueryHandler.boiCity.setValue(OrderQueryHandler.mgrData.selectionData[0].id);
}

/**
 * 加载区县名称
 * @param {} data
 */
OrderQueryHandler.loadDistrictData = function(data){
	OrderQueryHandler.boiDistrict.setData(null);
	OrderQueryHandler.boiDistrict.setData(data);
	OrderQueryHandler.boiDistrict.setValue(OrderQueryHandler.mgrData.selectionData[0].id);
}

/**
 * 加载指定的区域名称
 * @param {} bdrParent
 * @param {} level
 */
OrderQueryHandler.loadRegionData = function(bdrParent, level){
	$.ajax({
		url : OrderQueryHandler.mgrData.loadRegionInfoListUrl(null, bdrParent),
		cache : false,
		dataType : 'json',
		success : function(text) {
			var tempData = [OrderQueryHandler.mgrData.selectionData[0]];
				if(text != null && text != ""){
					for (var i = 0; i < text.length; i++) {
						var item = {
							id : text[i].bdrId,
							text : text[i].bdrName
						};
						tempData[i + 1] = item;
					}
				}
				
			if(level == null){
				OrderQueryHandler.loadCountryData(tempData);
			}else if(level == 1){
				OrderQueryHandler.loadProvinceData(tempData);
				OrderQueryHandler.loadCityData(OrderQueryHandler.mgrData.selectionData);
				OrderQueryHandler.loadDistrictData(OrderQueryHandler.mgrData.selectionData);
			}else if(level == 2){
				OrderQueryHandler.loadCityData(tempData);
				OrderQueryHandler.loadDistrictData(OrderQueryHandler.mgrData.selectionData);
			}else if(level == 3){
				OrderQueryHandler.loadDistrictData(tempData);
			}
			
		},
		error: function(){
			MiniuiTools.miniError("系统提示","数据加载出错！");
		},
		complete : function(){
			OrderQueryHandler.hiddenForm.unmask();
		}
	})
}

/**
 * 初始化数据
 */
OrderQueryHandler.init = function(){
	OrderQueryHandler.loadBoiOrderData(OrderQueryHandler.mgrData.boiOrderData);
	OrderQueryHandler.loadBoiPayData(OrderQueryHandler.mgrData.boiPayData);
	OrderQueryHandler.loadBoiShipData(OrderQueryHandler.mgrData.boiShipData);
	OrderQueryHandler.loadRegionData("0", null);
	OrderQueryHandler.loadProvinceData(OrderQueryHandler.mgrData.selectionData);
	OrderQueryHandler.loadCityData(OrderQueryHandler.mgrData.selectionData);
	OrderQueryHandler.loadDistrictData(OrderQueryHandler.mgrData.selectionData);
};	

OrderQueryHandler.init();

/**
 * 区域值改变事件处理程序
 * @param {} level
 */
OrderQueryHandler.selectRegion = function(level){
	OrderQueryHandler.hiddenForm.loading(); //loading
	
	if(level == 1){
		OrderQueryHandler.loadRegionData(OrderQueryHandler.boiCountry.getValue(), level);
	}else if(level == 2){
		OrderQueryHandler.loadRegionData(OrderQueryHandler.boiProvince.getValue(), level);
	}else if(level == 3){
		OrderQueryHandler.loadRegionData(OrderQueryHandler.boiCity.getValue(), level);
	}
}

/**
 * 高级查询订单点击事件处理程序
 */
OrderQueryHandler.higherQuery = function() {
	var orderQueryData = OrderQueryHandler.orderQueryForm.getData();
	orderQueryData = mini.encode(OrderQueryHandler.orderQueryForm.getData());
	
	window.location.href="orderList.jsp?isHigherQuery=true&queryingData=" + orderQueryData;
}

/**
 * 重置查询表单点击事件处理程序
 */
OrderQueryHandler.resetForm = function(){
	OrderQueryHandler.orderQueryForm.reset();
	OrderQueryHandler.init();
}

/**
 * 返回至订单列表页面
 */
OrderQueryHandler.returnOrderList = function(){
	window.location.href="orderList.jsp";
}